package wang.Seacher.seven.bean;

import org.apache.commons.lang3.StringUtils;

import com.geccocrawler.gecco.annotation.Attr;
import com.geccocrawler.gecco.annotation.Gecco;
import com.geccocrawler.gecco.annotation.Href;
import com.geccocrawler.gecco.annotation.HtmlField;
import com.geccocrawler.gecco.annotation.Image;
import com.geccocrawler.gecco.annotation.Request;
import com.geccocrawler.gecco.annotation.RequestParameter;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.spider.HtmlBean;

import lombok.Data;
import lombok.ToString;
import wang.Seacher.util.JudgeUtil;

/**
 * http://m.99mm.me/meitui/2427.html?url=3
 * @author WanG
 *
 */
@Data
@ToString
@Gecco(matchUrl="http://m.99mm.me/{topic}/{imgNum}.html?url={pageNum}", pipelines={"getPicBoxPipeline"})
public class picBox implements HtmlBean{

	/**
	 * @名称: picBox.java
	 * @包名： wang.Seacher.seven.bean
	 * @author： WanG
	 * @日期：2017年5月1日 下午2:36:58
	 */
	private static final long serialVersionUID = 1814312101814577665L;

	@Request
	private HttpRequest request;
	
	 /**
     * 图片编号
     */
    @RequestParameter
    private String imgNum;
    
    /**
     * 图片主题
     */
    @RequestParameter
    private String topic;
    
    @RequestParameter
    private String pageNum;
    
    
    /**
     * 图片
     */
    @HtmlField(cssPath="#picbox")
    private String img;
//    @HtmlField(cssPath="#picbox")
//    private List<PictureVo> pictureVo;
    
    
    
    
	@HtmlField(cssPath = "#picbox > a")
	private String biaoti;

	// 图片说明
	@Href(click=true)
	@HtmlField(cssPath = "#picbox > a")
	private String herf;
	
	// 图片
	@Image({"data-lazy-img", "src"})
    @HtmlField(cssPath="a > img")
	private String imgUrl;

	@Attr("width")
	@HtmlField(cssPath = "width")
	private String width;
	
	
	public void setBiaoti(String str) {
		if (!JudgeUtil.isPresent(str)) {
			return;
		}
		int alt = str.indexOf("alt");
		int one = str.indexOf("\"", alt);
		int two = str.indexOf("\"", one + 1);

		String substring = str.substring(one + 1, two);

		if (JudgeUtil.isPresent(pageNum)) {
			String removeStr = "(" + pageNum + ")";
			this.biaoti = StringUtils.replace(substring, removeStr, "");
		} else {
			this.biaoti = substring;
		}
	}

	public String getBiaoti() {
		return biaoti;
	}
	
	public PictureVo getPictureVo(){
		PictureVo pictureVo = new PictureVo();
		
		pictureVo.setBiaoti(biaoti);
		pictureVo.setHerf(herf);
		pictureVo.setImgUrl(imgUrl);
		pictureVo.setWidth(width);
		pictureVo.setTopic(topic);
		
		return pictureVo;
	}
	

}
